﻿namespace Sai.Test
{
    using Sai.Core.Introspection;
    using Sai.Test.TestDataBuilders;
    using Sai.Test.XUnit;
    using System.Collections.Generic;
    using Xunit;

	public class TypeNameFacts 
	{
		public class TheConstructor
		{
			
			[Fact, InMemoryTest]
			public void ParsesFullyQualifiedName()
			{
				var typeName = new TypeName(new FullyQualifiedName("System.Collections.Generic.List"));

				Assert.Equal("System.Collections.Generic", typeName.Namespace);
				Assert.Equal("List", typeName.Name);
				Assert.Equal("System.Collections.Generic.List", typeName.FullName.ToString());
			}

			
			[Fact, InMemoryTest]
			public void ParsesGlobalNamespaceName()
			{
				var typeName = new TypeName(new FullyQualifiedName("GlobalNamespaceClass"));

				Assert.Empty(typeName.Namespace);
				Assert.Equal("GlobalNamespaceClass", typeName.Name);
				Assert.True(typeName.InGlobalNamespace);
			}
		}

		public class TheToStringMethod
		{
			
			[Fact, InMemoryTest]
			public void ReturnsFullyQualifiedName()
			{
				var ExpectedName = new FullyQualifiedName("System.Collections.Generic.Dictionary<string, int>");
				var typeName = new TypeName(ExpectedName);

				Assert.Equal(ExpectedName.ToString(), typeName.ToString());
			}
		}

        public class Comparisons
        {
            [Fact, InMemoryTest]
            public void Single_Namespace_List_Sort_Returns_Ordered_By_Name()
            {
                var list = new List<TypeName>();
                list.Add(TypeNameBuilder.Type().Named("String").InNamespace("System").Build());
                list.Add(TypeNameBuilder.Type().Named("Int32").InNamespace("System").Build());
                list.Add(TypeNameBuilder.Type().Named("Int64").InNamespace("System").Build());

                list.Sort();

                Assert.Equal("Int32", list[0].Name);
                Assert.Equal("Int64", list[1].Name);
                Assert.Equal("String", list[2].Name);
            }

            [Fact, InMemoryTest]
            public void Sub_Namespaces_List_Sort_Returns_Ordered_By_Namespace_Then_Name()
            {
                var list = new List<TypeName>();
                list.Add(TypeNameBuilder.Type().Named("String").InNamespace("System").Build());
                list.Add(TypeNameBuilder.Type().Named("File").InNamespace("System.IO").Build());
                list.Add(TypeNameBuilder.Type().Named("Int32").InNamespace("System").Build());
                list.Add(TypeNameBuilder.Type().Named("Directory").InNamespace("System.IO").Build());
                list.Add(TypeNameBuilder.Type().Named("Int64").InNamespace("System").Build());
                list.Add(TypeNameBuilder.Type().Named("IQueryable").InNamespace("System.Linq").Build());

                list.Sort();

                Assert.Equal("Int32", list[0].Name);
                Assert.Equal("Int64", list[1].Name);
                Assert.Equal("String", list[2].Name);
                Assert.Equal("Directory", list[3].Name);
                Assert.Equal("File", list[4].Name);
                Assert.Equal("IQueryable", list[5].Name);

            }

        }

	}


}
